open(FH,"</Users/yaping/Documents/lab/project/SNP/Normal_SNP.txt") or die;
my @normal=<FH>;
chomp(@normal);
close(FH);
open(FH,"</Users/yaping/Documents/lab/project/SNP/Tumor_SNP.txt") or die;
my @tumor=<FH>;
chomp(@tumor);
close(FH);
open(FH,"</Users/yaping/Documents/lab/project/CNV-SNP-Methylation/normal.cnv3.bed") or die;
my @normal_cnv3=<FH>;
chomp(@normal_cnv3);
close(FH);
open(FH,"</Users/yaping/Documents/lab/project/CNV-SNP-Methylation/normal.cnv4.bed") or die;
my @normal_cnv4=<FH>;
chomp(@normal_cnv4);
close(FH);
open(FH,"</Users/yaping/Documents/lab/project/CNV-SNP-Methylation/tumor.cnv3.bed") or die;
my @tumor_cnv3=<FH>;
chomp(@tumor_cnv3);
close(FH);
open(FH,"</Users/yaping/Documents/lab/project/CNV-SNP-Methylation/tumor.cnv4.bed") or die;
my @tumor_cnv4=<FH>;
chomp(@tumor_cnv4);
close(FH);
my @out_normal;
my @out_tumor;
my $normal_filter=0;
my $tumor_filter=0;
my $normal_X_filter=0;
my $tumor_X_filter=0;
my $normal_het_filter=0;
my $tumor_het_filter=0;
my $normal_X_het_filter=0;
my $tumor_X_het_filter=0;
foreach my $snp(@normal){
	my @splitsnp=split "\t",$snp;
	push(@out_normal,"$snp\n");
	foreach my $cnv(@normal_cnv3){
		my @splitcnv=split "\t",$cnv;
		$splitcnv[0] =~ /chr(\S+)/;
		my $chr=$1;
		if($splitsnp[1] eq $chr && $splitsnp[2] >= ($splitcnv[1]-2000) && $splitsnp[2] <= ($splitcnv[2]+2000)){
			pop(@out_normal);
			$normal_filter++;
			$normal_X_filter++ if $chr eq "X";
			$normal_het_filter++ if $splitsnp[3] ne $splitsnp[4];
			$normal_X_het_filter++ if(($splitsnp[3] ne $splitsnp[4]) && ($chr eq "X"));
		}
	}
	foreach my $cnv(@normal_cnv4){
		my @splitcnv=split "\t",$cnv;
		$splitcnv[0] =~ /chr(\S+)/;
		my $chr=$1;
		if($splitsnp[1] eq $chr && $splitsnp[2] >= ($splitcnv[1]-2000) && $splitsnp[2] <= ($splitcnv[2]+2000)){
			pop(@out_normal);
			$normal_filter++;
			$normal_X_filter++ if $chr eq "X";
			$normal_het_filter++ if $splitsnp[3] ne $splitsnp[4];
			$normal_X_het_filter++ if(($splitsnp[3] ne $splitsnp[4]) && ($chr eq "X"));
		}
	}
}
print("$normal_filter\t$normal_X_filter\t$normal_het_filter\t$normal_X_het_filter\n");
open(OUT,">/Users/yaping/Documents/lab/project/SNP/Normal_SNP.filterCNV.txt");
print OUT @out_normal;
close(OUT);

foreach my $snp(@tumor){
	my @splitsnp=split "\t",$snp;
	push(@out_tumor,"$snp\n");
	foreach my $cnv(@tumor_cnv3){
		my @splitcnv=split "\t",$cnv;
		$splitcnv[0] =~ /chr(\S+)/;
		my $chr=$1;
		if($splitsnp[1] eq $chr && $splitsnp[2] >= ($splitcnv[1]-2000) && $splitsnp[2] <= ($splitcnv[2]+2000)){
			pop(@out_tumor);
			$tumor_filter++;
			$tumor_X_filter++ if $chr eq "X";
			$tumor_het_filter++ if $splitsnp[3] ne $splitsnp[4];
			$tumor_X_het_filter++ if(($splitsnp[3] ne $splitsnp[4]) && ($chr eq "X"));
		}
	}
	foreach my $cnv(@tumor_cnv4){
		my @splitcnv=split "\t",$cnv;
		$splitcnv[0] =~ /chr(\S+)/;
		my $chr=$1;
		if($splitsnp[1] eq $chr && $splitsnp[2] >= ($splitcnv[1]-2000) && $splitsnp[2] <= ($splitcnv[2]+2000)){
			pop(@out_tumor);
			$tumor_filter++;
			$tumor_X_filter++ if $chr eq "X";
			$tumor_het_filter++ if $splitsnp[3] ne $splitsnp[4];
			$tumor_X_het_filter++ if(($splitsnp[3] ne $splitsnp[4]) && ($chr eq "X"));
		}
	}
}
print("$tumor_filter\t$tumor_X_filter\t$tumor_het_filter\t$tumor_X_het_filter\n");
open(OUT,">/Users/yaping/Documents/lab/project/SNP/Tumor_SNP.filterCNV.txt");
print OUT @out_tumor;
close(OUT);

